const CumulationID = require('./id_alloter').CumulationID;

class Timer {
    constructor(){
        this._list = new Map();
        this._ids = new CumulationID();
        this._ids.init();
        this._handel = null;
    }
    //启动
    start(interval) {
        this._handel = setInterval(this._checkEvents, interval, this);
    }
    //停止
    stop() {
        if (this._handel) {
            clearInterval(this._handel);
        }
    }
    _checkEvents(self) {
        const now = parseInt(Date.now()/1000);
        let del = [];
        self._list.forEach(function (value, key) {
            if (value.timestamp < now) {
                value.callback();
                if (value.interval==0) {
                    del.push(key);
                    if (value.type==='monthly') {
                        const time = new DateTime(value.timestamp*1000);
                        const d = time.getDate();
                        const h = time.getHours();
                        const mm = time.getMinutes();
                        const s = time.getSeconds();
                        self.addMonthlyTimer(d, h, mm, s, value.callback);
                    } else if (value.type==='annually') {
                        const time = new DateTime(value.timestamp*1000);
                        const m = time.getMonth()+1;
                        const d = time.getDate();
                        const h = time.getHours();
                        const mm = time.getMinutes();
                        const s = time.getSeconds();
                        self.addAnnuallyTimer(m, d, h, mm, s, value.callback);
                    }
                } else {
                    value.timestamp += value.interval;
                }
            }
        });
        for (let i = 0; i < del.length; i++) {
            self._list.delete(del[i]);
        }
    }
    //添加时间戳计时器，单位秒
    addTimer(timestamp, interval, callback) {
        let info = {
            type: 'common',
            timestamp: timestamp,
            interval: interval,
            callback: callback
        }
        let id = this._ids.getID();
        this._list.set(id, info);
        return id;
    }
    //添加日期计时器，单位Date对象
    addDateTimer(date, interval, callback) {
        return this.addTimer(parseInt(date.valueOf()/1000), interval, callback);
    }
    //添加每日计时器
    addDailyTimer(h, m, s, callback) {
        let now = parseInt(Date.now()/1000);
        let ts = now-now%86400-8*3600+h*3600+m*60+s;
        if (ts <= now) {
            ts += 24*3600;
        }
        return this.addTimer(ts, 24*3600, callback);
    }
    //添加每月计时器
    addMonthlyTimer(d, h, m, s, callback) {
        const now = new Date();
        let year = now.getFullYear();
        let month = now.getMonth();
        let tag = new Date(year, month, d, h, m, s);
        if (tag.valueOf() <= now.valueOf()) {
            month++;
            if (month>11) {
                month -= 12;
                year++;
            }
            tag = new Date(year, month, d, h, m, s);
        }
        let info = {
            type: 'monthly',
            timestamp: parseInt(tag.valueOf()/1000),
            interval: 0,
            callback: callback
        }
        let id = this._ids.getID();
        this._list.set(id, info);
        return id;
    }
    //添加每年计时器
    addAnnuallyTimer(month, d, h, m, s, callback) {
        const now = new Date();
        let year = now.getFullYear();
        let tag = new Date(year, month-1, d, h, m, s);
        if (tag.valueOf() <= now.valueOf()) {
            tag = new Date(year+1, month-1, d, h, m, s);
        }
        let info = {
            type: 'annually',
            timestamp: parseInt(tag.valueOf()/1000),
            interval: 0,
            callback: callback
        }
        let id = this._ids.getID();
        this._list.set(id, info);
        return id;
    }
    //删除计时器
    delelte(id) {
        this._list.delete(id);
    }
}

module.exports = Timer;